<template>
  <div class="header">
    <div class="header-left">
      <img class="logo" src="@/assets/img/logo.svg" alt="" />
      <div class="web-title">后台管理系统</div>
      <!-- 菜单折叠按钮 -->
      <div class="collapse-btn" @click="collapseChange">
        <el-icon v-if="sidebar.collapse">
          <Expand />
        </el-icon>
        <el-icon v-else>
          <Fold />
        </el-icon>
      </div>
    </div>
    <div class="header-right">
      <div class="header-user-con">
        <div class="btn-icon" @click="router.push('/user-center')">
          <el-tooltip
            effect="dark"
            :content="message ? `有${message}条未读消息` : `消息中心`"
            placement="bottom"
          >
            <el-icon>
              <Notification />
            </el-icon>
          </el-tooltip>
          <span v-if="message" class="btn-bell-badge"></span>
        </div>
        <div class="btn-icon" @click="setFullScreen">
          <el-tooltip effect="dark" content="全屏" placement="bottom">
            <el-icon>
              <FullScreen />
            </el-icon>
          </el-tooltip>
        </div>
        <!-- 用户头像 -->
        <el-avatar class="user-avatar" :size="30" :src="userStore.avatarUrl" />
        <!-- 用户名下拉菜单 -->
        <el-dropdown class="user-name" trigger="click" @command="handleCommand">
          <span class="el-dropdown-link">
            {{ username }}
            <el-icon class="el-icon--right">
              <ArrowDown />
            </el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="user-center">个人中心</el-dropdown-item>
              <el-dropdown-item divided command="login-out">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
  import { onMounted } from 'vue'
  import { useSidebarStore } from '@/store/sidebar'
  import { useRouter } from 'vue-router'
  import { ArrowDown, Expand, Fold, FullScreen, Notification } from '@element-plus/icons-vue'
  import { localStorageDelItem, localStorageGetItem } from '@/utils/local-storage-util.ts'
  import { LS_USER } from '@/constant/local-storage-key.ts'
  import { User } from '@/types'
  import { useUserStore } from '@/store/user.ts'

  const user: User | null = localStorageGetItem(LS_USER)
  const username: string = user ? user.username : ''
  const message: number = 2
  const userStore = useUserStore()
  const sidebar = useSidebarStore()
  // 侧边栏折叠
  const collapseChange = () => {
    sidebar.handleSwitchCollapse()
  }

  onMounted(() => {
    //小屏幕自动折叠
    if (document.body.clientWidth < 1500) {
      sidebar.handleSetCollapse(true)
    }
  })

  // 用户名下拉菜单选择事件
  const router = useRouter()
  const handleCommand = (command: string) => {
    if (command == 'login-out') {
      localStorageDelItem(LS_USER)
      router.push('/login')
    } else if (command == 'user-center') {
      router.push('/user-center')
    }
  }

  const setFullScreen = () => {
    if (document.fullscreenElement) {
      document.exitFullscreen()
    } else {
      document.body.requestFullscreen.call(document.body)
    }
  }
</script>
<style scoped>
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-sizing: border-box;
    width: 100%;
    height: 70px;
    color: var(--header-text-color);
    background-color: var(--header-bg-color);
    border-bottom: 1px solid #ddd;
  }

  .header-left {
    display: flex;
    align-items: center;
    padding-left: 20px;
    height: 100%;
  }

  .logo {
    width: 35px;
  }

  .web-title {
    margin: 0 40px 0 10px;
    font-size: 22px;
  }

  .collapse-btn {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    padding: 0 10px;
    cursor: pointer;
    opacity: 0.8;
    font-size: 22px;
  }

  .collapse-btn:hover {
    opacity: 1;
  }

  .header-right {
    float: right;
    padding-right: 50px;
  }

  .header-user-con {
    display: flex;
    height: 70px;
    align-items: center;
  }

  .btn-icon {
    position: relative;
    width: 30px;
    height: 30px;
    text-align: center;
    cursor: pointer;
    display: flex;
    align-items: center;
    color: var(--header-text-color);
    margin: 0 5px;
    font-size: 20px;
  }

  .btn-bell-badge {
    position: absolute;
    right: 4px;
    top: 0;
    width: 8px;
    height: 8px;
    border-radius: 4px;
    background: #f56c6c;
    color: var(--header-text-color);
  }

  .user-avatar {
    margin: 0 10px 0 20px;
  }

  .el-dropdown-link {
    color: var(--header-text-color);
    cursor: pointer;
    display: flex;
    align-items: center;
  }
</style>
